/**
 * @author Linrui
 * @since 2011-03-07
 * 新闻资讯的页面逻辑控制
 */
package com.qeado.news.web;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.qeado.news.model.News;
import com.qeado.news.service.NewsService;
import com.qeado.sort.model.Sort;
import com.qeado.sort.service.SortService;
import com.qeado.util.PageHelper;

@Scope("session")
@Controller
public class NewsAction extends ActionSupport {

	@Autowired
	private SortService sortService;
	@Autowired
	private NewsService newsService;
	
	private String id; // 新闻文章的ID
	private PageHelper nationalNews; // 国内新闻
	private PageHelper internationalNews; // 国际新闻
	private PageHelper policyNews; // 政策法规
	private PageHelper newsListBySort; // 用来加载子类别的文章
	private Map<String, PageHelper> allNews; // 加载其他二级类别下的所有第一页新闻

	private List<Sort> sortList; // 新闻类别
	private List<Sort> leafSortList; // 叶子节点的二级新闻类别
	private List<Sort> sortList_pv; // 地方新闻类别
	private List<News> newList_hot; // 热门新闻排行
	private List<Sort> sortList_aspect; // 行业新闻类别
	
	private int pageNum; // 页码
	private Sort sort; // 文章的分类编码
	private News news; // 用来加载某篇文章，显示其具体信息

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public Sort getSort() {
		return sort;
	}

	public void setSort(Sort sort) {
		this.sort = sort;
	}

	public PageHelper getNewsListBySort() {
		return newsListBySort;
	}

	public void setNewsListBySort(PageHelper newsListBySort) {
		this.newsListBySort = newsListBySort;
	}

	public SortService getSortService() {
		return sortService;
	}

	public void setSortService(SortService sortService) {
		this.sortService = sortService;
	}

	public NewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public PageHelper getNationalNews() {
		return nationalNews;
	}

	public void setNationalNews(PageHelper nationalNews) {
		this.nationalNews = nationalNews;
	}

	public PageHelper getInternationalNews() {
		return internationalNews;
	}

	public void setInternationalNews(PageHelper internationalNews) {
		this.internationalNews = internationalNews;
	}

	public PageHelper getPolicyNews() {
		return policyNews;
	}

	public void setPolicyNews(PageHelper policyNews) {
		this.policyNews = policyNews;
	}

	public Map<String, PageHelper> getAllNews() {
		return allNews;
	}

	public void setAllNews(Map<String, PageHelper> allNews) {
		this.allNews = allNews;
	}

	public List<Sort> getSortList() {
		return sortList;
	}

	public void setSortList(List<Sort> sortList) {
		this.sortList = sortList;
	}

	public List<Sort> getLeafSortList() {
		return leafSortList;
	}

	public void setLeafSortList(List<Sort> leafSortList) {
		this.leafSortList = leafSortList;
	}

	public List<Sort> getSortList_pv() {
		return sortList_pv;
	}

	public void setSortList_pv(List<Sort> sortListPv) {
		sortList_pv = sortListPv;
	}

	public List<News> getNewList_hot() {
		return newList_hot;
	}

	public void setNewList_hot(List<News> newListHot) {
		newList_hot = newListHot;
	}

	public List<Sort> getSortList_aspect() {
		return sortList_aspect;
	}

	public void setSortList_aspect(List<Sort> sortListAspect) {
		sortList_aspect = sortListAspect;
	}

	@Override
	public String execute() throws Exception {
		sortList = sortService.listByPcode("NE"); // 取到所有的二级之类别
		leafSortList = sortService.listLeafSortByPid(sortList.get(1)
				.getParentsId()); // 取到所有二级叶子子类别，意味着有新闻记录
		Sort temp;
		String sortName;
		allNews = new HashMap<String,PageHelper>();
		for (Iterator iter = leafSortList.iterator(); iter.hasNext();) {
			temp = (Sort) iter.next();
			PageHelper pageHelper = newsService.listBySortByPage(1, temp
					.getTypeCode());
			sortName = temp.getTypeName();	
			if (sortName.equals("国内新闻"))
				nationalNews = pageHelper;
			else if (sortName.equals("国际新闻"))
				internationalNews = pageHelper;
			else if (sortName.equals("政策法规"))
				policyNews = pageHelper;
			else {
				allNews.put(sortName, pageHelper);
			}
		}
		sortList_pv = sortService.listByPcode("NE010");
		sortList_aspect = sortService.listByPcode("NE004");
		return super.execute();
	}

	/* 分页查询所有新闻 */
	public String listByPage(int pageNum) {
		// PageHelper = newsService.listByPage(pageNum);
		return "success";
	}

	/* 按类别分页查询新闻 */
	public String listBySortByPage() {
		sortList = sortService.listByPcode("NE"); // 取到所有的二级之类别		
		newsListBySort = newsService.listBySortByPage(pageNum, sort.getTypeCode());
		newsListBySort.setPagebar("news!listBySortByPage?sort.typeCode=" + sort.getTypeCode());
		sort.setTypeName( sortService.getNameByCode( sort.getTypeCode()) );
		return "listBySortByPage_success";
	}
	
	/* 按ID查询新闻 */
	public String queryById() {
		news = newsService.queryById(id);
		return "queryById_success";
	}

}
